package org.hxyjs.dao.impl;

import org.hxyjs.dao.NewsRecordDao;
import org.hxyjs.entity.NewsRecord;
import org.hxyjs.mapper.NewsRecordMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("newsRecordDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class NewsRecordDaoImpl implements NewsRecordDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private NewsRecordMapper newsRecordMapper;

    @Override
    public List<NewsRecord> getNewsRecordByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) newsRecordMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return newsRecordMapper.getNewsRecordByPage(map);
    }


    public List<NewsRecord> getAllNewsRecords() throws Exception {
        return newsRecordMapper.selectByExample(null);
    }

    @Override
    public NewsRecord getNewsRecordByNewno(String newno) throws Exception {
        return newsRecordMapper.selectByPrimaryKey(newno);
    }


    @Override
    public Integer deleteNewsRecordByNewno(String newno) {
        return newsRecordMapper.deleteByPrimaryKey(newno);
    }

    @Override
    public Integer updateNewsRecordByNewno(NewsRecord newsRecord) {

        return newsRecordMapper.updateByPrimaryKey(newsRecord);
    }

    @Override
    public Integer addNewsRecord(NewsRecord newsRecord) {
        newsRecord.setNewno(initId());
        return newsRecordMapper.insertSelective(newsRecord);
    }

    public String initId() {
        String maxNewno = newsRecordMapper.getMaxNewno();
        return GenNumTools.initId("11", 3, maxNewno);
    }

}